/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 26, 2011
 * @Createtime 4:55:06 PM
 */

package com.tmh.study.aS3DP.mcv.weatherMap
{
	import com.tmh.study.aS3DP.mcv.minimalist.CompositeView;

	import flash.events.Event;

	import mx.collections.ArrayCollection;

	import spark.components.ComboBox;

	public class CBView extends CompositeView
	{
		private var cb:ComboBox;

		public function CBView(model:Object, controller:Object = null)
		{
			super(model, controller);

			// get region names from model
			var aRegions:Array = (model as IModel).getRegionList();

			// draw combo box using region names
			cb = new ComboBox();
			cb.width = 200;
			cb.move(100, 10);

			var cbDP:ArrayCollection = new ArrayCollection();

			for (var i:uint = 0; i < aRegions.length; i++)
			{
				cbDP.addItem({label:aRegions[i], data:i});
			}

			cb.dataProvider = cbDP;

			update();
			addChild(cb);

			// register to receive change to combo box
			cb.addEventListener(Event.CHANGE, changeHandler);
		}

		override public function update(event:Event = null):void
		{
			cb.selectedIndex = (_model as IModel).getRegion();
			super.update(event);
		}

		public function changeHandler(event:Event):void
		{
			// delegate to the controller (strategy) to handle
			(_controller as ICompInputHandler).compChangeHandler(ComboBox(event.target).selectedItem.data);
		}
	}
}